#tk scaling 1.2


wm title . "Checkbox tanks"
wm resizable . 0 0


ttk::frame .header -relief sunken

#menu
menu .mainMenu -tearoff 0
.mainMenu add command -label "Connect"    -command connectWindow
.mainMenu add command -label "Disconnect" -command { dropConnection; bench1_core 0; set statusText "Offline"; }
.mainMenu add separator
.mainMenu add command -label "Help" -command { helpMessage }
.mainMenu add command -label "Exit" -command { destroy . }

#menubutton
ttk::menubutton .header.menuButton -text "Menu" -menu .mainMenu -takefocus 0

#statusbar
ttk::label .header.statusBar -textvariable statusText -anchor center -width 12 

#spawn buttons
ttk::labelframe .spawnButtons    -text "Respawn" -padding "5" 
ttk::button     .spawnButtons.b1 -text "1" -command { tankCmd s 1 } -takefocus 0
ttk::button     .spawnButtons.b2 -text "2" -command { tankCmd s 2 } -takefocus 0
ttk::button     .spawnButtons.b3 -text "3" -command { tankCmd s 3 } -takefocus 0 
ttk::button     .spawnButtons.b4 -text "4" -command { tankCmd s 4 } -takefocus 0

#battelfield
ttk::labelframe .battlefield -padding "5 0 5 5" -text ":)"
for { set i 0 } { $i < 200 } { incr i } {
	ttk::checkbutton ".battlefield.f$i" -padding "0 0 0 0" -variable "fld$i" -takefocus 0
}

set i 0
for { set row 0 } { $row < 20 } { incr row } {
	for { set col 0 } { $col < 10 } { incr col } {
		grid ".battlefield.f$i" -row $row -column $col
		incr i
	}
}


#attach all elements to window
grid .header            -row 0 -column 0 -sticky we 
grid .header.menuButton -row 0 -column 0 -sticky w
grid .header.statusBar  -row 0 -column 1 -sticky e -pady 5

grid .spawnButtons    -row 1 -column 0 -pady 2
grid .spawnButtons.b1 -row 0 -column 0
grid .spawnButtons.b2 -row 0 -column 1
grid .spawnButtons.b3 -row 1 -column 0
grid .spawnButtons.b4 -row 1 -column 1

grid .battlefield -row 2 -column 0 -padx 5 -pady 5 



#functions
proc connect2server {} {
	
	set resmsg 0
	
	.connectWindow.frame1.ip       state disabled 
	.connectWindow.frame1.port     state disabled
	.connectWindow.buttons.connect state disabled
	.connectWindow.buttons.cancel  state disabled
	
	if { ![winfo exist .connectWindow.process] } {
		ttk::label .connectWindow.process
		grid .connectWindow.process -row 1 -column 0 -pady 10
	}
	
	.connectWindow.process configure -text "Connecting..."
	
	connect2serverCore
	
	if { $resmsg != 0 } {
		connect2serverRes $resmsg
	}
	
}

proc connect2serverRes { res } {
	
	if { ![winfo exists .connectWindow] } { return }
	
	if { $res != 0 } {
		.connectWindow.process configure -text $res
		
		.connectWindow.frame1.ip       state !disabled 
		.connectWindow.frame1.port     state !disabled
		.connectWindow.buttons.connect state !disabled
		.connectWindow.buttons.cancel  state !disabled
	} else {
		destroy .connectWindow
	}
	
}

proc connectWindow {} {
	
	if { [winfo exists .connectWindow] } { return }
	
	toplevel .connectWindow -relief raised
	
	wm title .connectWindow "Connect to server"
	wm resizable .connectWindow 0 0
	
	ttk::labelframe .connectWindow.frame1 -padding "5" -text "Server address"
	
	ttk::label .connectWindow.frame1.iplabel   -text "IP"
	ttk::label .connectWindow.frame1.portlabel -text "Port"
	ttk::entry .connectWindow.frame1.ip   -justify center -width 17 -textvariable serverIp
	ttk::entry .connectWindow.frame1.port -justify center -width 17 -textvariable serverPort
	
	ttk::frame  .connectWindow.buttons
	ttk::button .connectWindow.buttons.connect -text "Connect" -command { connect2server  }
	ttk::button .connectWindow.buttons.cancel  -text "Cancel"  -command { destroy .connectWindow }
	
	grid .connectWindow.frame1 -row 0 -padx 10 -pady 5
	
	grid .connectWindow.frame1.iplabel   -row 0 -column 0 -padx 5
	grid .connectWindow.frame1.ip        -row 0 -column 1 -padx 5
	grid .connectWindow.frame1.portlabel -row 1 -column 0 -padx 5
	grid .connectWindow.frame1.port      -row 1 -column 1 -padx 5
	
	grid .connectWindow.buttons -row 2 -column 0 -pady 5
	
	grid .connectWindow.buttons.connect -row 0 -column 0 -padx 10
	grid .connectWindow.buttons.cancel  -row 0 -column 1 -padx 10
	
	bind .connectWindow <Return>   { .connectWindow.buttons.connect invoke }
	bind .connectWindow <KP_Enter> { .connectWindow.buttons.connect invoke }
	bind .connectWindow <Escape>   { .connectWindow.buttons.cancel  invoke }
	
	raise .connectWindow
	focus -force .connectWindow
	focus .connectWindow.frame1.ip
}
 
proc helpMessage { } {
	
	tk_messageBox -title "Help" -message "Controls" -detail "1,2,3,4 - spawn\nw,a,s,d or arrows - move\nspace - shot"
}


set savedKey 0
set savedTime 0

proc tankControlDown { key timestamp } {
	
	global savedKey
	global savedTime
	
	if { $key != $savedKey || $timestamp != $savedTime } {
		tankControl $key 1
	} else {
		after cancel tankControl $key 0
	}
}

proc tankControlUp { key timestamp } {
	
	global savedKey
	global savedTime
	
	set savedKey $key
	set savedTime $timestamp
	
	after 5 tankControl $key 0
}


#key binding
bind . <Control-q> { destroy . }

bind . <space> { tankCmd f f }

bind . <Key-1> { .spawnButtons.b1 invoke }
bind . <Key-2> { .spawnButtons.b2 invoke }
bind . <Key-3> { .spawnButtons.b3 invoke }
bind . <Key-4> { .spawnButtons.b4 invoke }

bind . <KeyPress-w>     { tankControlDown u %t }
bind . <KeyPress-a>     { tankControlDown l %t }
bind . <KeyPress-s>     { tankControlDown d %t }
bind . <KeyPress-d>     { tankControlDown r %t }

bind . <KeyPress-Up>    { tankControlDown u %t }
bind . <KeyPress-Left>  { tankControlDown l %t }
bind . <KeyPress-Down>  { tankControlDown d %t }
bind . <KeyPress-Right> { tankControlDown r %t }

bind . <KeyRelease-w>     { tankControlUp u %t }
bind . <KeyRelease-a>     { tankControlUp l %t }
bind . <KeyRelease-s>     { tankControlUp d %t }
bind . <KeyRelease-d>     { tankControlUp r %t }

bind . <KeyRelease-Up>    { tankControlUp u %t }
bind . <KeyRelease-Left>  { tankControlUp l %t }
bind . <KeyRelease-Down>  { tankControlUp d %t }
bind . <KeyRelease-Right> { tankControlUp r %t }





#open connect to server dialog
connectWindow


set statusText "Offline"

ttk::style layout TCheckbutton {
	Checkbutton.indicator
}
